package org.springblade.modules.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.dto.plan.PlanContentExcelSearchDTO;
import org.springblade.modules.admin.service.PlanContentExcelService;
import org.springblade.modules.admin.vo.plan.PerformanceVoV2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zcc
 * @version 1.0
 * @date 2021/10/22 16:27
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/plan/excel")
@Api(value = "任务下载excel相关", tags = "任务下载excel相关")
public class PlanContentExcelController {

	private final PlanContentExcelService planContentExcelService;


	@PostMapping("/getPerformance")
	@ApiOperation("任務統計")
	public R<IPage> getPerformanceV2(@RequestBody PlanContentExcelSearchDTO planContentExcelSearchDTO) {
		Query query = new Query();
		query.setSize(planContentExcelSearchDTO.getSize());
		query.setCurrent(planContentExcelSearchDTO.getCurrent());
		IPage iPage = Condition.getPage(query);
		iPage.setRecords(planContentExcelService.getPerformance(planContentExcelSearchDTO,iPage));
		return R.data(iPage);
	}

	@PostMapping("/getPerformance/down")
	public void getPerformance(HttpServletResponse response, @RequestBody PlanContentExcelSearchDTO planContentExcelSearchDTO) throws IOException {
		List<PerformanceVoV2> performanceV2 = planContentExcelService.getPerformance(planContentExcelSearchDTO,null);
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList("姓名","在职状态", "职位", "所在组", "计划电梯数",
				"完成电梯数", "工作量(计划)", "工作量(完成)", "及时工作量","及时率(%)","推迟率(%)",
				"任务签到(次)", "任务签退(次)", "平均作业时长(分钟)",
				"应打卡次数", "正常打卡次数","迟到打卡次数", "早退打卡次数", "异常定位总次数","补卡总次数","缺卡次数","加班次数","加班工时统计(分钟)");
			rows.add(title);
			for (int i = 0; i < performanceV2.size(); i++) {
				PerformanceVoV2 performanceVoV2 = performanceV2.get(i);
				List<String> row = new ArrayList<>();
				row.add(Convert.toStr(performanceVoV2.getUserName()));
				row.add(Convert.toStr(performanceVoV2.getUserStatusName()));
				row.add(Convert.toStr(performanceVoV2.getRoleName()));
				row.add(Convert.toStr(performanceVoV2.getGroupName()));
				row.add(Convert.toStr(performanceVoV2.getMaintainElevatorCount()));

				row.add(Convert.toStr(performanceVoV2.getFinishElevatorCount()));
				row.add(Convert.toStr(performanceVoV2.getPlanWorkload()));
				row.add(Convert.toStr(performanceVoV2.getFinishWorkload()));
				row.add(Convert.toStr(performanceVoV2.getInTimeWorkload()));
				row.add(Convert.toStr(performanceVoV2.getInTimeWorkloadRatio().multiply(new BigDecimal(100)))+"%");
				row.add(Convert.toStr(performanceVoV2.getWorkloadPostponeRatio().multiply(new BigDecimal(100)))+"%");

				row.add(Convert.toStr(performanceVoV2.getSignCounts()));
				row.add(Convert.toStr(performanceVoV2.getSignOutCounts()));
				row.add(Convert.toStr(performanceVoV2.getAvgWorkMinute()));
				row.add(Convert.toStr(performanceVoV2.getSignDetailCounts()));
				row.add(Convert.toStr(performanceVoV2.getNormalSignDetailCounts()));

				row.add(Convert.toStr(performanceVoV2.getBeLateCount()));
				row.add(Convert.toStr(performanceVoV2.getSignOutLeaveEarlyCount()));
				row.add(Convert.toStr(performanceVoV2.getSignAbnormalCount()));
				row.add(Convert.toStr(performanceVoV2.getUpdateSignCount()));
				row.add(Convert.toStr(performanceVoV2.getDisSignCount()));
				row.add(Convert.toStr(performanceVoV2.getOvertimeCount()));
				row.add(Convert.toStr(performanceVoV2.getOvertime()));
				rows.add(row);
			}

			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (
			Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}






	/**
	 * 功能描述: 发送响应流方法
	 */
	private void setResponseHeader(HttpServletResponse response, String fileName) {
		try {
			try {
				fileName = new String(fileName.getBytes(), "ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			response.setContentType("application/octet-stream;charset=ISO8859-1");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.addHeader("Pargam", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
